package com.example.grape_effect.effet.filter

import com.example.grape_effect.effet.effect.LYEffect
import com.example.grape_effect.gl.common.ShaderConstant
import com.example.grape_effect.gl.common.ShaderParameter

class DistortFilter: LYEffect() {
    init {
        name = "distortFilter"
    }
    override var vertexShader = ShaderConstant.NO_FILTER_VERTEX_SHADER
    override var fragmentShader =  """
        precision highp float;
        varying vec2 textureCoordinate;
        uniform sampler2D inputTexture;
        uniform sampler2D inputTexture2;
        uniform float intensity;

        void main()
         {
            vec2 dir = textureCoordinate - vec2(0.6,0.6);
        // 最终偏移的值：方向 * （1-长度），越靠外偏移越小
            vec2 offset = intensity * normalize(dir)* (1.0- length(dir) );
            vec2 uv =  textureCoordinate + offset;
            gl_FragColor = texture2D( inputTexture,uv);
         }

    """.trimIndent()

    override fun addParameters() {
        super.addParameters()
        parameters["intensity"] = ShaderParameter(
            "intensity",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
    }

    override fun setDefaultParameterValues() {
        super.setDefaultParameterValues()
        parameters["intensity"]?.value = 0.1f
    }

    var isSmallScale = true
    var intensity = 0f
    fun updateIntensity() {
            if (isSmallScale) {
                intensity = intensity + 0.1f
            } else {
            intensity = intensity- 0.1f;
            }
            if (intensity >= 1f) {
                isSmallScale = false
            } else if (intensity <= -1.0f) {
                isSmallScale = true
            }

        parameters["intensity"]?.value = intensity
    }

    override fun draw() {
        updateIntensity()
        super.draw()
    }
}